package dealfunc

import (
	"bufio"
	"net/http"
	"os"
	"github.com/gin-gonic/gin"
)

// 读文件并返回数组
func readComment() ([][]string, error) {
	file, err := os.Open("commentData.txt")
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var dataArr [][]string
	scanner := bufio.NewScanner(file)

	// 扫描文件
	for scanner.Scan() {
		item := [2]string{}
		item[0] = scanner.Text()
		// 下一行
		for scanner.Scan() {
			if scanner.Text() == "!@#$%^&*()_+" {
				break
			}
			item[1] += scanner.Text()
		}
		dataArr = append(dataArr, item[:])
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return dataArr, nil
}

func GetComment(c *gin.Context) {
	dataArr, err := readComment()

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"result": "false",
			"message": "haven't com",
		})
		return
	}

	// 成功
	c.JSON(http.StatusAccepted, gin.H{
		"result": "true",
		"data": dataArr,
	})
}